package org.dync.giftlibrary.util.glide;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * Created by KathLine on 2017/12/12.
 */

public class GlideLoader {
    private Object mImageUrlObj;
    private RequestOptions mRequestOptions;

    public static GlideLoader init() {
        return new GlideLoader();
    }

    private GlideLoader() {
    }

    public GlideLoader load(Object url) {
        mImageUrlObj = url;
        return this;
    }

    public GlideLoader applyDefault(int placeholderId, int errorId) {
        mRequestOptions = new RequestOptions()
                .centerCrop()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        if (placeholderId != 0) {
            mRequestOptions.placeholder(placeholderId);
        }
        if (errorId != 0) {
            mRequestOptions.error(errorId);
        }
        return this;
    }

    public GlideLoader applyDefault(int errorId) {
        return applyDefault(0, errorId);
    }

    public GlideLoader applyDefault() {
        return applyDefault(0, 0);
    }

    /**
     * 例子
     * <pre>
     * RequestOptions options = new RequestOptions()
     * .centerCrop()
     * .priority(Priority.HIGH)
     * .diskCacheStrategy(DiskCacheStrategy.ALL);
     * </pre>
     *
     * @param requestOptions
     * @return GlideLoader
     */
    public GlideLoader apply(RequestOptions requestOptions) {
        mRequestOptions = requestOptions;
        return this;
    }

    public GlideLoader bitmapTransform(Transformation<PixelMap> transformation) {
        apply(RequestOptions.bitmapTransform(transformation));
        return this;
    }

    /**
     * 最后调用
     *
     * @param context
     * @param listener
     * @return RequestBuilder<Element>
     */
    public RequestBuilder<Element> listener(Context context, RequestListener<Element> listener) {
        return Glide.with(context)
                .load(mImageUrlObj)
                .apply(mRequestOptions)
                .listener(listener);
    }

    /**
     * 最后调用
     *
     * @param imageView
     * @return Target<Element>
     */
    public Target<Element> into(Image imageView) {
        return Glide.with(imageView.getContext())
                .load(mImageUrlObj)
                .apply(mRequestOptions)
                .into(imageView);
    }

    public Target<Element> into(Context context, Target<Element> target) {
        return Glide.with(context)
                .load(mImageUrlObj)
                .apply(mRequestOptions)
                .into(target);
    }

}
